package com.ljh.extensions.rabbit.listener;

import com.ljh.extensions.rabbit.constants.Constants;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author: Themberfue
 * @date: 2025/4/30 15:57
 * @description:
 */
@Component
public class AckListener {
    @RabbitListener(queues = Constants.ACK_QUEUE)
    public void process(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.printf("接收到消息：%s，deliveryTag：%d\n",
                new String(message.getBody(), "UTF-8"),
                deliveryTag);

        try {
            System.out.println("模拟处理业务逻辑");
//            int a = 3 / 0;
            System.out.println("模拟处理业务完成");

            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            channel.basicNack(deliveryTag, false, true);
        }
    }
}
